package com.tjsinfo.tjsoa.mail.bo;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.tjsinfo.tjsoa.mail.dao.ITjsDeleteEmailDao;
import com.tjsinfo.tjsoa.mail.dao.ITjsFoldMailDao;
import com.tjsinfo.tjsoa.mail.vo.TjsDeleteEmail;
import com.tjsinfo.tjsoa.mail.vo.TjsDeleteEmailAnnex;
import com.tjsinfo.tjsoa.mail.vo.TjsDraft;
import com.tjsinfo.tjsoa.mail.vo.TjsDraftAnnex;
import com.tjsinfo.tjsoa.mail.vo.TjsEmailFolder;
import com.tjsinfo.tjsoa.mail.vo.TjsFoldEmailAnnex;
import com.tjsinfo.tjsoa.mail.vo.TjsFoldMail;
import com.tjsinfo.tjsoa.mail.vo.TjsemailReceived;

/**
 * 删除邮件的BO
 * @author wangxiaowei
 *
 */
public class TjsDeleteEmailBo implements ITjsDeleteEmailBo{
	private final boolean STARTTRUE=true;
	private final boolean STARTFALSE=false;
	private ITjsDeleteEmailDao tjsDeleteEmailDao;
	private ITjsFoldMailDao tjsFoldMailDao;
	
	public ITjsFoldMailDao getTjsFoldMailDao() {
		return tjsFoldMailDao;
	}

	public void setTjsFoldMailDao(ITjsFoldMailDao tjsFoldMailDao) {
		this.tjsFoldMailDao = tjsFoldMailDao;
	}

	public ITjsDeleteEmailDao getTjsDeleteEmailDao() {
		return tjsDeleteEmailDao;
	}

	public void setTjsDeleteEmailDao(ITjsDeleteEmailDao tjsDeleteEmailDao) {
		this.tjsDeleteEmailDao = tjsDeleteEmailDao;
	}

	public boolean addTjsDeleteEmail(TjsDeleteEmail tjsDeleteEmail) {
		// TODO Auto-generated method stub
		try {
			List<TjsDeleteEmail> list = tjsDeleteEmailDao.findAll();
			
			tjsDeleteEmailDao.save(tjsDeleteEmail);
			return STARTTRUE;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return STARTFALSE;
		}
	}

	public boolean deleteTjsDeleteEmail(TjsDeleteEmail tjsDeleteEmail) {
		// TODO Auto-generated method stub
		try {
			tjsDeleteEmailDao.delete(tjsDeleteEmail);
			return STARTTRUE;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return STARTFALSE;
		}
	}

	public TjsDeleteEmail selectById(String id) {
		// TODO Auto-generated method stub
		return tjsDeleteEmailDao.selectById(id);
	}
	/**
	 * 查询用户的所有的删除邮件
	 */
	public List<TjsDeleteEmail> selectByUserId(Integer userid) {
		// TODO Auto-generated method stub
		return tjsDeleteEmailDao.selectByUserId(userid);
	}
	/**
	 * 修改邮件的读写状态
	 * @param array
	 * @param start
	 * @return
	 */
	public boolean updateDeleteMailStart(String[] array,Integer start)
	{
		try {
			for (int i = 0; i < array.length; i++) {
				//查询数组中的删除的邮件
				TjsDeleteEmail deleteEmail = tjsDeleteEmailDao
						.selectById(array[i]);
				//改变读写状态
				deleteEmail.setTjsmailStart(start);
				tjsDeleteEmailDao.update(deleteEmail);
			}
			return STARTTRUE;
		} catch (Exception e) {
			// TODO: handle exception
			return STARTFALSE;
		}
		
	}
	/**
	 * 修改邮件的类型
	 * @param array
	 * @param start
	 * @return
	 */
	public boolean updateDeleteMailType(String[] array,Integer type)
	{
		try{
		for (int i = 0; i < array.length; i++) {
			//查询数组中的删除的邮件
			TjsDeleteEmail deleteEmail = tjsDeleteEmailDao
					.selectById(array[i]);
			//改变类型
			deleteEmail.setTjsmailType(type);
			tjsDeleteEmailDao.update(deleteEmail);
		}
		return STARTTRUE;
	} catch (Exception e) {
		// TODO: handle exception
		return STARTFALSE;
	}
	}
	
	/**
	 * 修改邮件的标签
	 * @param array
	 * @param start
	 * @return
	 */
	public boolean updateDeleteMailTag(String[] array,Integer type)
	{
		try{
		for (int i = 0; i < array.length; i++) {
			//查询数组中的删除的邮件
			TjsDeleteEmail deleteEmail = tjsDeleteEmailDao
					.selectById(array[i]);
			//改变类型标签
			deleteEmail.setTismailTag(type);
			tjsDeleteEmailDao.update(deleteEmail);
		}
		return STARTTRUE;
	} catch (Exception e) {
		// TODO: handle exception
		return STARTFALSE;
	}
	}
	/**
	 * 批量删除邮件
	 * @param array
	 * @return
	 */
	public boolean deleteEmailList(String[] array)
	{
			try {
				for (int i = 0; i < array.length; i++) {
					
					deleteTjsDeleteEmail(selectById(array[i] ));
				}
				return STARTTRUE;
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return STARTFALSE;
			}
	}
	/**
	 * 删除邮件移动
	 * @param array
	 * @param foldid
	 * @return
	 */
	public boolean movedeleteEmail(String[] array, String foldid) {
		// TODO Auto-generated method stub
		try {
			for (int i = 0; i < array.length; i++) {
				TjsDeleteEmail received = selectById(array[i]);//查询删除的邮件
				TjsFoldMail tjsFoldMail = new TjsFoldMail();//声明一个文件夹邮件
				tjsFoldMail.setTjsmailRead(received.getTismailRead());
				tjsFoldMail.setTjsmailTag(received.getTismailTag());
				tjsFoldMail.setTjsmailDatetime(new Date());
				tjsFoldMail.setTjsmailStart(0);
				tjsFoldMail.setTjsmailTitle(received.getTjsmailTitle());
				tjsFoldMail.setTjsmailType(received.getTjsmailType());
				tjsFoldMail.setTjsmailValue(received.getTjsmailValue());
				TjsEmailFolder tjsEmailFolder = new TjsEmailFolder();
				tjsEmailFolder.setFolderId(foldid);
				tjsFoldMail.setTjsEmailFolder(tjsEmailFolder);
				tjsFoldMail.setTjsEmailFolder(tjsEmailFolder);
				tjsFoldMail.setTjsmailUsername(received.getTjsmailUsername());
				Set set2 = received.getTjsDeleteEmailAnnexes();//获得这个邮件的附件
				for (Iterator iterator = set2.iterator(); iterator.hasNext();) {
					TjsDeleteEmailAnnex annex = (TjsDeleteEmailAnnex) iterator.next();
					TjsFoldEmailAnnex   emailAnnex = new TjsFoldEmailAnnex();
					emailAnnex.setAnnexAddress(annex.getAnnexAddress());
					emailAnnex.setAnnexName(annex.getAnnexName());
					emailAnnex.setAnnexSize(annex.getAnnexSize());
					emailAnnex.setAnnexZhenName(annex.getZhenname());
					tjsFoldMail.addTjsFoldMailAnnex(emailAnnex);
				}
				tjsFoldMailDao.save(tjsFoldMail);//保存这个文件夹邮件
				tjsDeleteEmailDao.delete(received);//删除这个邮件
				}
				return STARTTRUE;
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return STARTFALSE;
		}
		
	}
	/**
	 * 查询一个删除邮件内容
	 * @param id
	 * @return
	 */
	public TjsDeleteEmail selectOneIDSUB(String id)
	{
		List<TjsDeleteEmail> list = tjsDeleteEmailDao.selectByIdSUB(id);
		if (list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}
	/**
	 * 根据标题查询
	 * @param 标题
	 * @return
	 */
	public List<TjsDeleteEmail> selectByTitle(String title,Integer userid)
	{
		Object[] object = {"%"+title+"%",userid};
		return  tjsDeleteEmailDao.selectByTitle(object);
	}
}
